/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.entities.Services;
import model.entities.RoleSV;
import model.entities.Room;
import model.entities.RoomType;


/**
 *
 * @author Hieu.Nguyen
 */
public class ServicesDAO {
    private static ServicesDAO servicesDAO;

    public static ServicesDAO getInstance() {
        if (servicesDAO == null) {
            servicesDAO = new ServicesDAO();
        }
        return servicesDAO;
    }
    
   
     public List<Services> getServices(boolean isActived) {
        List<Services> listServices = new ArrayList<Services>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblServices where isActived = 'true'";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Services r = new Services();
                r.setServicesID(rs.getInt("ServicesID"));
                r.setServicesName(rs.getString("ServicesName"));
                
                RoleSV aa = RoleSVDAO.getInstance().getRoleSVByID(rs.getInt("RoleSVID"));                
                r.setRoleSV(aa);
                
                r.setServicesAmount(rs.getFloat("ServicesAmount"));
                
                
               
                listServices.add(r);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listServices;
    }
    
               

     public boolean createServices(Services services) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "insert into tblServices values (?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
           
            pstmt.setString(1, services.getServicesName());
            pstmt.setInt(2, services.getRoleSV().getRoleSVID());
            pstmt.setFloat(3, services.getServicesAmount());
            pstmt.setBoolean(4, services.isIsActived());
            pstmt.executeUpdate();
            isCheck = true;
                isCheck = true;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return isCheck;
     }
        
     public Services  getServicesByID(int servicesID) {
        Services  rm = new Services ();
       
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblServices where ServicesID = ? ";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, servicesID);            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                
                rm.setServicesID(rs.getInt("ServicesID"));
                rm.setServicesName(rs.getString("ServicesName"));                
                RoleSV rt = RoleSVDAO.getInstance().getRoleSVByID(rs.getInt("RoleSVID"));
                rm.setRoleSV(rt);
                rm.setServicesAmount(rs.getFloat("ServicesAmount"));
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return rm;
     }
     
     public boolean deleteServices(int servicesID) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "update tblServices set isActived = 'false' where ServicesID = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, servicesID);

            pstmt.executeUpdate();
            isCheck = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isCheck;
    }
     public boolean updateRoom(Services services) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "update tblServices set ServicesName = ?, RoleSVID = ?, ServicesAmount = ?, where ServicesID = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, services.getServicesName());
            pstmt.setInt(2, services.getRoleSV().getRoleSVID());
            pstmt.setFloat(3, services.getServicesAmount());
            pstmt.setInt(4, services.getServicesID());

            pstmt.executeUpdate();
            isCheck = true;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return isCheck;
    }
    
}
